package parasoft.centrap.test;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.Map;


public class Timer {
	public static boolean printall = false;
	private static Timestamp now = new Timestamp(System.currentTimeMillis());
	private static final String MAIN = "MAIN";
	private static Map<String, Long> start = new HashMap<String, Long>(){{
		put(MAIN, System.currentTimeMillis());
	}};
	private static Map<String, Long> stop = new HashMap<String, Long>();
	private static String lastKey = "MAIN";
	
	private static Timestamp gd(){
		now.setTime(System.currentTimeMillis());
		return now;
	}
	private static Timestamp gd(long time){
		now.setTime(time);
		return now;
	}
	public static void tstart(){
		tstart(MAIN);
	}
	public static void tstart(String key){
		start.put(key, System.currentTimeMillis());
		if(printall)System.err.println("==>>"+key+"\t\t"+gd());
	}
	public static void tstop(){
		tstop(MAIN, false);
	}
	public static void tstop(boolean stopAll){
		tstop(MAIN, stopAll);
	}
	public static void tstop(String key){
		tstop(key, false);
	}
	public static void tstop(String key, boolean stopAll){
		if(stopAll){
			long tstop = System.currentTimeMillis();
			for (String km : stop.keySet()) {
				stop.put(km, tstop);
			}
		}else{
			stop.put(key, System.currentTimeMillis());
		}
		if(printall)System.err.println("<<=="+key+"\t\t"+gd());
	}
	public static double getTime(){
		return getTime(MAIN);
	}
	public static double getTime(String key){
		Long lstart = stop.get(key);
		Long lstop =  start.get(key);
		if(lstart==null || lstop==null){
			return 0;
		}
		double time = (double) (lstart - lstop) / 1000;
		System.err.println("\n==========================================");
		System.err.println("\t\t"+key+"\t\t\t\t");
		System.err.println("\t\tstart:\t\t\t\t"+gd(lstart)+" sec.");
		System.err.println("\t\tstop:\t\t\t\t"+gd(lstop)+" sec.");
		System.err.println("\t\ttime:\t\t\t\t"+time+" sec.");
		System.err.println("==========================================\n");
		return time;
	}
	public static void summary(){
		for (String km : stop.keySet()) {
			getTime(km);
		}
	}
}
